﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using TSL_Tools.SqlTools;
using ReportingEngine;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;

namespace VSLT_Reporting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private void GetScorecardInfo()
        {
            System.Windows.Input.Cursor _cursor = this.Cursor;
            this.Cursor = Cursors.Wait;

            // Add a summary table to the top of the report
            BuildScorecardRunSummaryTable();

            // Add page comparison table
            BuildPageComparisonTable();

            // Add scorecard Combo charts to report
            //GetAllScorecardComboCharts(true);

            //AddSectionTitleToReport("Individual ScoreCard Charts");
            //this.Cursor = _cursor;
            //foreach (ScoreCardItem item in scoreCardFields)
            //{
            //    // This is needed when switching to the ReportGenerator engine
            //    //Chart chart = this.FindName("MyWinformChart") as Chart;
            //    //LoadChartTemplate(ref chart, "Templates\\ScoreCard_Chart_Template.xml");

            //    if (BuildScorecardSingleItemChart(item))
            //        AddCurrentChartToReport();
            //}
            btnSaveReport.IsEnabled = true;
            this.Cursor = _cursor;
        }

        private void BuildScorecardRunSummaryTable()
        {
            if (_scoreCardMetrics != null)
            {
                _scoreCardMetrics.Rows.Clear();
                _scoreCardMetrics.Columns.Clear();
            }
            else
            {
                _scoreCardMetrics = new DataTable();
            }

            DataTable table2;

            // Get the summary for the first run and add it to the final table
            bool bUsePerfmon = (bool)cbUsePerfmonForScorecard.IsChecked;
            _scoreCardMetrics = re.GetSummaryTimingResultsForReportAsTable(selectedRuns.Min(), bUsePerfmon);

            for (int x = 1; x < selectedRuns.Count; x++)
            {
                table2 = re.GetSummaryTimingResultsForReportAsTable(selectedRuns[x], bUsePerfmon);
                _scoreCardMetrics.Merge(table2);
                table2.Rows.Clear();
                table2.Columns.Clear();
            }

            dgRunResults.ItemsSource = _scoreCardMetrics.AsDataView();
            CopyRunResultsTableToClipboard();
            ClearReportTab();
            BuildScoreCardTable();

        }

        private void BuildScoreCardTable()
        {
            DataTable descriptionTable = new DataTable();
            descriptionTable.Columns.Add("RunId", typeof(System.Int32));
            descriptionTable.Columns.Add("TestName", typeof(System.String));
            descriptionTable.Columns.Add("StartTime", typeof(System.DateTime));
            descriptionTable.Columns.Add("Duration", typeof(System.Int32));
            descriptionTable.Columns.Add("Description", typeof(System.String));

            for (int x = 0; x < _scoreCardMetrics.Rows.Count; x++)
            {
                descriptionTable.Rows.Add();
                descriptionTable.Rows[x]["RunId"] = _scoreCardMetrics.Rows[x]["RunId"];
                descriptionTable.Rows[x]["TestName"] = _scoreCardMetrics.Rows[x]["TestName"];
                descriptionTable.Rows[x]["StartTime"] = _scoreCardMetrics.Rows[x]["StartTime"];
                descriptionTable.Rows[x]["Duration"] = _scoreCardMetrics.Rows[x]["Duration"];
                descriptionTable.Rows[x]["Description"] = _scoreCardMetrics.Rows[x]["Description"];
            }

            rtbResults.Document = CreateScorecardXamlTableFromDataTable("", descriptionTable);
            //CopyRunResultsTableToClipboard();
        }

        private void BuildPageComparisonTable(string comparisonField = "Average")
        {
            DataTable _table = GetPageComparisonTable(comparisonField);

            FlowDocument _doc = rtbResults.Document;
            _doc.Blocks.Add(ReportGenerator.TSL_ReportGenerator.AddSectionTitleToReport("Page Comparison Summary and Heatmap"));

            Typeface font = new Typeface("Arial");
            ReportGenerator.CellHighlighting cellHighlight = new ReportGenerator.CellHighlighting();
            cellHighlight.ReadHighlightSettingsFromXmlFile(Properties.Settings.Default.ChartObjectFileName);

            _doc.Blocks.Add(ReportGenerator.TSL_ReportGenerator.CreateXamlTableFromDataTable("Page Comparison", _table, font, 10, cellHighlight).Blocks.FirstBlock);            
        }

        private DataTable GetPageComparisonTable(string comparisonField)
        {
            StringBuilder runListInputStringForSqlQuery = new StringBuilder(256);
            for (int x = 0; x < selectedRuns.Count; x++)
            {
                runListInputStringForSqlQuery.Append("[" + selectedRuns[x] + "],");
            }
            runListInputStringForSqlQuery.Remove(runListInputStringForSqlQuery.Length - 1, 1);  // remove the final comma

            DataTable _table = new DataTable();
            TalkToSql tts = new TalkToSql(re.sqlConn);
            string sqlQuery = String.Format(Sql_GETSCORECARDPAGECOMPARISONTABLE, runListInputStringForSqlQuery.ToString(), comparisonField);
            tts.ExecuteReaderTable(sqlQuery, out _table);
            this.mostRecentSqlQuery = sqlQuery;

            return _table;
        }
    }
}



